var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");
var openHelper = require("helper/open");
var http = require("helper/http");
var userUtil = require("helper/user");
var urlUser = require("url/user");
var nf = deviceone.sm("do_Notification");
var imgTool = require("helper/picture");
var imHelper = require("helper/im");

var circleUrl = require("url/circle");
var httpUrl4Detail = circleUrl.url.base.detail4manage;
var updateCircleUrl = circleUrl.url.base.updateOpenLevel;

var circleId = null;
var openLevel = null;
page.on("loaded", function() {
	circleId = page.getData().circleId;
	getCircleInfo();
});
//andriod返回键
page.on("back", function() {
	app.closePage();
});

var layoutHead = ui("layoutHead");
var addheader = layoutHead.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
  app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
  "labelTitle" : "管理圈子"
})
header.refreshData();

var aoutFirst = ui("aoutFirst");
var aoutSecond = ui("aoutSecond");

/*编辑圈资料*/
var addEditInfo = aoutFirst.add("editInfo", "source://view/reuse/mutiLabel3.ui",0,0);
var editInfo = ui("editInfo");
editInfo.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "编辑圈资料",
    "labelTipsVisible":true,
    "labelTips":''
});
editInfo.refreshData();
editInfo.on("touch", function(data, e) {
	openHelper.open("source://view/circle/base/manage/circleEdit.ui",{"circleId":circleId});
});
//间隔线
var addLine = aoutFirst.add("line", "source://view/reuse/form/line.ui",addEditInfo);
var line = ui("line");
line.refreshData();

/*管理成员*/
var addSetManager = aoutFirst.add("setManager", "source://view/reuse/mutiLabel3.ui",addLine);
var setManager = ui("setManager");
setManager.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "管理成员",
    "labelTipsVisible":true,
    "labelTips":''
});
setManager.refreshData();
setManager.on("touch", function(data, e) {
	if(circleId!=null){
		openHelper.open("source://view/circle/member/manage/memberList.ui",{circleId:circleId});
	}
});

/*设置成员头衔*/
var addSetMemberGrade = aoutFirst.add("setMemberGrade", "source://view/reuse/mutiLabel3.ui",addSetManager);
var setMemberGrade = ui("setMemberGrade");
setMemberGrade.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "设置成员头衔",
    "labelTipsVisible":true,
    "labelTips":''
});
setMemberGrade.refreshData();
setMemberGrade.on("touch", function(data, e) {
	if(circleId!=null){
		openHelper.open("source://view/circle/base/manageOne/memberGradeSet.ui",{circleId:circleId});
	}
});

/*加圈方式*/
var addJoinCircleType = aoutFirst.add("joinCircleType", "source://view/reuse/mutiLabel3.ui",setMemberGrade);
var joinCircleType = ui("joinCircleType");
joinCircleType.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "加圈方式",
    "labelTipsVisible":true,
    "labelTips":''
});
joinCircleType.refreshData();

//设置加圈方式--begin
var addPicker = ui("do_ALayout_root").add("joinTypePicker","source://view/reuse/form/oneCategory.ui", 0, 0);
var joinTypePicker = ui("joinTypePicker");
page.on("refreshOneCategory", function(data) {
	if ("joinTypeSelect" == data[1]) {
		var typeId = data[0].oneid;
		var typeName = data[0].onename;
		ui(addJoinCircleType+".labelTips").text = typeName;
		openLevel = typeId;
		if(openLevel != null && openLevel>="1" && openLevel <="3"){
			doUpdateOpenLevel();
		}
		
	}
});
//设置加圈方式--end
joinCircleType.refreshData();
joinCircleType.on("touch", function(data, e) {
	//1-完全公开(自由加入);2-半开放(申请加入);3-私密(受邀请加入)
	var ptitle = {"title" : "请选择加圈方式","id" : "joinTypeSelect"};
	var pcount = [ ptitle, [{"id":1,name:"完全公开(自由加入)"},{"id":2,name:"半开放(申请加入)"},{"id":3,name:"私密(受邀请加入)"}]];
	page.fire("oneCategoryList", pcount);
	page.fire("oneCategory", "true");
});
//间隔线
var addLine2 = aoutFirst.add("line2", "source://view/reuse/form/line.ui",addJoinCircleType);

/*设置圈内禁止发布活动*/
var addNoActivity = aoutFirst.add("noActivity", "source://view/reuse/mutiLabel3.ui",addLine2);
var noActivity = ui("noActivity");
noActivity.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "圈内禁止发布活动",
    "labelTipsVisible":true,
    "labelTips":''
});
noActivity.refreshData();
noActivity.on("touch", function(data, e) {
	openHelper.open("source://view/circle/base/manageOne/noActivitySet.ui",{"circleId":circleId});
});

/*圈内禁止发布投票*/
var addNoVote = aoutFirst.add("noVote", "source://view/reuse/mutiLabel3.ui",addNoActivity);
var noVote = ui("noVote");
noVote.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "圈内禁止发布投票",
    "labelTipsVisible":true,
    "labelTips":''
});
noVote.refreshData();
noVote.on("touch", function(data, e) {
	openHelper.open("source://view/circle/base/manageOne/noVoteSet.ui",{"circleId":circleId});
});


function getCircleInfo(){
	http.post(httpUrl4Detail,{id:circleId},function(data){
		var circle = data.circleBase;
		if(circle){
			openLevel = circle.openLevel;
			ui(addJoinCircleType+".labelTips").text = circle.openLevelName;
		}
	});
}

function doUpdateOpenLevel(){
	var circle = {
			"id":circleId,
			"openLevel":openLevel
			};
	http.post(updateCircleUrl,circle,function(data){
		nf.toast("设置“加圈方式”成功");
	});
}

